/*
 * ServerConfig.cpp
 * 
 * 服务器配置实现
 * 使用 nlohmann/json 进行 JSON 解析
 */

#include "common/config/ServerConfig.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>

namespace framework {

using json = nlohmann::json;

bool ServerConfig::loadFromFile(const std::string& filename) {
    try {
        std::ifstream file(filename);
        if (!file.is_open()) {
            std::cerr << "Failed to open config file: " << filename << std::endl;
            return false;
        }
        
        json j;
        file >> j;
        
        // 读取基本配置
        serverId = j.value("serverId", 0);
        serverType = static_cast<ServerType>(j.value("serverType", 1));
        serverName = j.value("serverName", "");
        logLevel = j.value("logLevel", "INFO");
        
        // 读取网络配置
        if (j.contains("network")) {
            auto& n = j["network"];
            network.ip = n.value("ip", "0.0.0.0");
            network.port = n.value("port", 8080);
            network.threadNum = n.value("threadNum", 0);
            network.maxConnections = n.value("maxConnections", 10000);
            network.heartbeatInterval = n.value("heartbeatInterval", 30);
        }
        
        std::cout << "Config loaded successfully from: " << filename << std::endl;
        std::cout << "  Server ID: " << serverId << std::endl;
        std::cout << "  Server Name: " << serverName << std::endl;
        std::cout << "  Listen: " << network.ip << ":" << network.port << std::endl;
        std::cout << "  Thread Num: " << network.threadNum << std::endl;
        
        return true;
        
    } catch (const json::parse_error& e) {
        std::cerr << "JSON parse error: " << e.what() << std::endl;
        std::cerr << "At position: " << e.byte << std::endl;
        return false;
    } catch (const json::exception& e) {
        std::cerr << "JSON error: " << e.what() << std::endl;
        return false;
    } catch (const std::exception& e) {
        std::cerr << "Error loading config: " << e.what() << std::endl;
        return false;
    }
}

bool ServerConfig::saveToFile(const std::string& filename) const {
    try {
        json j = {
            {"serverId", serverId},
            {"serverType", static_cast<int>(serverType)},
            {"serverName", serverName},
            {"network", {
                {"ip", network.ip},
                {"port", network.port},
                {"threadNum", network.threadNum},
                {"maxConnections", network.maxConnections},
                {"heartbeatInterval", network.heartbeatInterval}
            }},
            {"logLevel", logLevel}
        };
        
        std::ofstream file(filename);
        if (!file.is_open()) {
            std::cerr << "Failed to create config file: " << filename << std::endl;
            return false;
        }
        
        file << j.dump(4);  // 缩进 4 空格
        std::cout << "Config saved successfully to: " << filename << std::endl;
        return true;
        
    } catch (const std::exception& e) {
        std::cerr << "Error saving config: " << e.what() << std::endl;
        return false;
    }
}

} // namespace framework

